

/*
	Base Data Access Layer.
	(C) Lehavi Solutions	

*/
	
#region using	
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
#endregion


namespace DAL
{	
	
	public partial class Accounts
	{

		#region Basic CRUD
		// Create / Update / Delete
		protected internal static int Insert(BO.Account account)
		{
			var cmd =  @"
				insert into [Accounts]
					([MasterAccountID], [Name], [TwUserID], [TwToken], [TwSecret], [TwFollowersNum], [FbUid], [FbToken], [FbSessionKey], [FbFriendNum], [FbPageID], [FbPageName], [FbPageToken], [FbPageFanNum], [YtToken], [YtUsername], [YtPassword], [WpUsername], [WpPassword], [WpUrl], [FlickrToken], [DateCreated], [LastModified], [StatusEnum])
				values
					(@MasterAccountID, @Name, @TwUserID, @TwToken, @TwSecret, @TwFollowersNum, @FbUid, @FbToken, @FbSessionKey, @FbFriendNum, @FbPageID, @FbPageName, @FbPageToken, @FbPageFanNum, @YtToken, @YtUsername, @YtPassword, @WpUsername, @WpPassword, @WpUrl, @FlickrToken, @DateCreated, @LastModified, @StatusEnum);
				
				select scope_identity();
				";
				
			var parameters = new SqlParameter[24]; 
			
			if (account.MasterAccountID == null)
				parameters[0] = new SqlParameter("MasterAccountID",DBNull.Value);
			else
				parameters[0] = new SqlParameter("MasterAccountID",account.MasterAccountID);
			if (account.Name == null)
				parameters[1] = new SqlParameter("Name",DBNull.Value);
			else
				parameters[1] = new SqlParameter("Name",account.Name);
			if (account.TwUserID == null)
				parameters[2] = new SqlParameter("TwUserID",DBNull.Value);
			else
				parameters[2] = new SqlParameter("TwUserID",account.TwUserID);
			if (account.TwToken == null)
				parameters[3] = new SqlParameter("TwToken",DBNull.Value);
			else
				parameters[3] = new SqlParameter("TwToken",account.TwToken);
			if (account.TwSecret == null)
				parameters[4] = new SqlParameter("TwSecret",DBNull.Value);
			else
				parameters[4] = new SqlParameter("TwSecret",account.TwSecret);
			parameters[5] = new SqlParameter("TwFollowersNum",account.TwFollowersNum );
			if (account.FbUid == null)
				parameters[6] = new SqlParameter("FbUid",DBNull.Value);
			else
				parameters[6] = new SqlParameter("FbUid",account.FbUid);
			if (account.FbToken == null)
				parameters[7] = new SqlParameter("FbToken",DBNull.Value);
			else
				parameters[7] = new SqlParameter("FbToken",account.FbToken);
			if (account.FbSessionKey == null)
				parameters[8] = new SqlParameter("FbSessionKey",DBNull.Value);
			else
				parameters[8] = new SqlParameter("FbSessionKey",account.FbSessionKey);
			parameters[9] = new SqlParameter("FbFriendNum",account.FbFriendNum );
			if (account.FbPageID == null)
				parameters[10] = new SqlParameter("FbPageID",DBNull.Value);
			else
				parameters[10] = new SqlParameter("FbPageID",account.FbPageID);
			if (account.FbPageName == null)
				parameters[11] = new SqlParameter("FbPageName",DBNull.Value);
			else
				parameters[11] = new SqlParameter("FbPageName",account.FbPageName);
			if (account.FbPageToken == null)
				parameters[12] = new SqlParameter("FbPageToken",DBNull.Value);
			else
				parameters[12] = new SqlParameter("FbPageToken",account.FbPageToken);
			parameters[13] = new SqlParameter("FbPageFanNum",account.FbPageFanNum );
			if (account.YtToken == null)
				parameters[14] = new SqlParameter("YtToken",DBNull.Value);
			else
				parameters[14] = new SqlParameter("YtToken",account.YtToken);
			if (account.YtUsername == null)
				parameters[15] = new SqlParameter("YtUsername",DBNull.Value);
			else
				parameters[15] = new SqlParameter("YtUsername",account.YtUsername);
			if (account.YtPassword == null)
				parameters[16] = new SqlParameter("YtPassword",DBNull.Value);
			else
				parameters[16] = new SqlParameter("YtPassword",account.YtPassword);
			if (account.WpUsername == null)
				parameters[17] = new SqlParameter("WpUsername",DBNull.Value);
			else
				parameters[17] = new SqlParameter("WpUsername",account.WpUsername);
			if (account.WpPassword == null)
				parameters[18] = new SqlParameter("WpPassword",DBNull.Value);
			else
				parameters[18] = new SqlParameter("WpPassword",account.WpPassword);
			if (account.WpUrl == null)
				parameters[19] = new SqlParameter("WpUrl",DBNull.Value);
			else
				parameters[19] = new SqlParameter("WpUrl",account.WpUrl);
			if (account.FlickrToken == null)
				parameters[20] = new SqlParameter("FlickrToken",DBNull.Value);
			else
				parameters[20] = new SqlParameter("FlickrToken",account.FlickrToken);
			parameters[21] = new SqlParameter("DateCreated",account.DateCreated );
			parameters[22] = new SqlParameter("LastModified",account.LastModified );
			if (account.StatusEnum == null)
				parameters[23] = new SqlParameter("StatusEnum",DBNull.Value);
			else
				parameters[23] = new SqlParameter("StatusEnum",account.StatusEnum);
			
			return Convert.ToInt32(DBHelper.GetScalar(cmd, CommandType.Text, parameters));
		}
		
		protected internal static void Update(BO.Account account)
		{
			var cmd = @"
				update [Accounts] set
					MasterAccountID = @MasterAccountID, Name = @Name, TwUserID = @TwUserID, TwToken = @TwToken, TwSecret = @TwSecret, TwFollowersNum = @TwFollowersNum, FbUid = @FbUid, FbToken = @FbToken, FbSessionKey = @FbSessionKey, FbFriendNum = @FbFriendNum, FbPageID = @FbPageID, FbPageName = @FbPageName, FbPageToken = @FbPageToken, FbPageFanNum = @FbPageFanNum, YtToken = @YtToken, YtUsername = @YtUsername, YtPassword = @YtPassword, WpUsername = @WpUsername, WpPassword = @WpPassword, WpUrl = @WpUrl, FlickrToken = @FlickrToken, DateCreated = @DateCreated, LastModified = @LastModified, StatusEnum = @StatusEnum
				where ID = @ID
				";
				
			var parameters = new SqlParameter[25];
			parameters[0] = new SqlParameter("ID",account.ID );
			if (account.MasterAccountID == null)
				parameters[1] = new SqlParameter("MasterAccountID",DBNull.Value);
			else
				parameters[1] = new SqlParameter("MasterAccountID",account.MasterAccountID);
			if (account.Name == null)
				parameters[2] = new SqlParameter("Name",DBNull.Value);
			else
				parameters[2] = new SqlParameter("Name",account.Name);
			if (account.TwUserID == null)
				parameters[3] = new SqlParameter("TwUserID",DBNull.Value);
			else
				parameters[3] = new SqlParameter("TwUserID",account.TwUserID);
			if (account.TwToken == null)
				parameters[4] = new SqlParameter("TwToken",DBNull.Value);
			else
				parameters[4] = new SqlParameter("TwToken",account.TwToken);
			if (account.TwSecret == null)
				parameters[5] = new SqlParameter("TwSecret",DBNull.Value);
			else
				parameters[5] = new SqlParameter("TwSecret",account.TwSecret);
			parameters[6] = new SqlParameter("TwFollowersNum",account.TwFollowersNum );
			if (account.FbUid == null)
				parameters[7] = new SqlParameter("FbUid",DBNull.Value);
			else
				parameters[7] = new SqlParameter("FbUid",account.FbUid);
			if (account.FbToken == null)
				parameters[8] = new SqlParameter("FbToken",DBNull.Value);
			else
				parameters[8] = new SqlParameter("FbToken",account.FbToken);
			if (account.FbSessionKey == null)
				parameters[9] = new SqlParameter("FbSessionKey",DBNull.Value);
			else
				parameters[9] = new SqlParameter("FbSessionKey",account.FbSessionKey);
			parameters[10] = new SqlParameter("FbFriendNum",account.FbFriendNum );
			if (account.FbPageID == null)
				parameters[11] = new SqlParameter("FbPageID",DBNull.Value);
			else
				parameters[11] = new SqlParameter("FbPageID",account.FbPageID);
			if (account.FbPageName == null)
				parameters[12] = new SqlParameter("FbPageName",DBNull.Value);
			else
				parameters[12] = new SqlParameter("FbPageName",account.FbPageName);
			if (account.FbPageToken == null)
				parameters[13] = new SqlParameter("FbPageToken",DBNull.Value);
			else
				parameters[13] = new SqlParameter("FbPageToken",account.FbPageToken);
			parameters[14] = new SqlParameter("FbPageFanNum",account.FbPageFanNum );
			if (account.YtToken == null)
				parameters[15] = new SqlParameter("YtToken",DBNull.Value);
			else
				parameters[15] = new SqlParameter("YtToken",account.YtToken);
			if (account.YtUsername == null)
				parameters[16] = new SqlParameter("YtUsername",DBNull.Value);
			else
				parameters[16] = new SqlParameter("YtUsername",account.YtUsername);
			if (account.YtPassword == null)
				parameters[17] = new SqlParameter("YtPassword",DBNull.Value);
			else
				parameters[17] = new SqlParameter("YtPassword",account.YtPassword);
			if (account.WpUsername == null)
				parameters[18] = new SqlParameter("WpUsername",DBNull.Value);
			else
				parameters[18] = new SqlParameter("WpUsername",account.WpUsername);
			if (account.WpPassword == null)
				parameters[19] = new SqlParameter("WpPassword",DBNull.Value);
			else
				parameters[19] = new SqlParameter("WpPassword",account.WpPassword);
			if (account.WpUrl == null)
				parameters[20] = new SqlParameter("WpUrl",DBNull.Value);
			else
				parameters[20] = new SqlParameter("WpUrl",account.WpUrl);
			if (account.FlickrToken == null)
				parameters[21] = new SqlParameter("FlickrToken",DBNull.Value);
			else
				parameters[21] = new SqlParameter("FlickrToken",account.FlickrToken);
			parameters[22] = new SqlParameter("DateCreated",account.DateCreated );
			parameters[23] = new SqlParameter("LastModified",account.LastModified );
			if (account.StatusEnum == null)
				parameters[24] = new SqlParameter("StatusEnum",DBNull.Value);
			else
				parameters[24] = new SqlParameter("StatusEnum",account.StatusEnum);
			DBHelper.ExecuteNonQuery(cmd, CommandType.Text, parameters);
		}
		
		protected internal static void Delete(object ID)
		{
			var cmd = "delete [Accounts] where ID = @ID";
			
			DBHelper.ExecuteNonQuery(cmd, CommandType.Text, new SqlParameter("ID", ID));
		}

		#endregion 
			
		internal static DataTable Select(string cmd, SqlParameter[] parameters)
		{
			return DBHelper.GetTable(cmd, parameters);
		}
		
		internal static int Count(string cmd, SqlParameter[] parameters)
		{
			return Convert.ToInt32(DBHelper.GetScalar(cmd, parameters));
		}
		
		internal static void SetValue(object id, string columnName, object value)
		{
			var cmd = string.Format("update [Accounts] set [{0}] = @Value where ID = @ID", columnName);
			
			DBHelper.ExecuteNonQuery(cmd, new SqlParameter("Value", value ?? DBNull.Value), new SqlParameter("ID", id));
		}
		
		internal static void SafeDetach(object fkID, string columnName)
		{
			var cmd = string.Format("update [Accounts] set [{0}] = null where [{0}] = @FkID", columnName);

			DBHelper.ExecuteNonQuery(cmd, new SqlParameter("FkID", fkID));
		}
	
	}
			
	
	public partial class AccountStats
	{

		#region Basic CRUD
		// Create / Update / Delete
		protected internal static long Insert(BO.AccountStat accountStat)
		{
			var cmd =  @"
				insert into [AccountStats]
					([AccountID], [FbPageFanNum], [FbFriendsNum], [TwFollowersNum], [Date])
				values
					(@AccountID, @FbPageFanNum, @FbFriendsNum, @TwFollowersNum, @Date);
				
				select scope_identity();
				";
				
			var parameters = new SqlParameter[5]; 
			
			parameters[0] = new SqlParameter("AccountID",accountStat.AccountID );
			parameters[1] = new SqlParameter("FbPageFanNum",accountStat.FbPageFanNum );
			parameters[2] = new SqlParameter("FbFriendsNum",accountStat.FbFriendsNum );
			parameters[3] = new SqlParameter("TwFollowersNum",accountStat.TwFollowersNum );
			parameters[4] = new SqlParameter("Date",accountStat.Date );
			
			return Convert.ToInt64(DBHelper.GetScalar(cmd, CommandType.Text, parameters));
		}
		
		protected internal static void Update(BO.AccountStat accountStat)
		{
			var cmd = @"
				update [AccountStats] set
					AccountID = @AccountID, FbPageFanNum = @FbPageFanNum, FbFriendsNum = @FbFriendsNum, TwFollowersNum = @TwFollowersNum, Date = @Date
				where ID = @ID
				";
				
			var parameters = new SqlParameter[6];
			parameters[0] = new SqlParameter("ID",accountStat.ID );
			parameters[1] = new SqlParameter("AccountID",accountStat.AccountID );
			parameters[2] = new SqlParameter("FbPageFanNum",accountStat.FbPageFanNum );
			parameters[3] = new SqlParameter("FbFriendsNum",accountStat.FbFriendsNum );
			parameters[4] = new SqlParameter("TwFollowersNum",accountStat.TwFollowersNum );
			parameters[5] = new SqlParameter("Date",accountStat.Date );
			DBHelper.ExecuteNonQuery(cmd, CommandType.Text, parameters);
		}
		
		protected internal static void Delete(object ID)
		{
			var cmd = "delete [AccountStats] where ID = @ID";
			
			DBHelper.ExecuteNonQuery(cmd, CommandType.Text, new SqlParameter("ID", ID));
		}

		#endregion 
			
		internal static DataTable Select(string cmd, SqlParameter[] parameters)
		{
			return DBHelper.GetTable(cmd, parameters);
		}
		
		internal static int Count(string cmd, SqlParameter[] parameters)
		{
			return Convert.ToInt32(DBHelper.GetScalar(cmd, parameters));
		}
		
		internal static void SetValue(object id, string columnName, object value)
		{
			var cmd = string.Format("update [AccountStats] set [{0}] = @Value where ID = @ID", columnName);
			
			DBHelper.ExecuteNonQuery(cmd, new SqlParameter("Value", value ?? DBNull.Value), new SqlParameter("ID", id));
		}
		
		internal static void SafeDetach(object fkID, string columnName)
		{
			var cmd = string.Format("update [AccountStats] set [{0}] = null where [{0}] = @FkID", columnName);

			DBHelper.ExecuteNonQuery(cmd, new SqlParameter("FkID", fkID));
		}
	
	}
			
	
	public partial class MasterAccounts
	{

		#region Basic CRUD
		// Create / Update / Delete
		protected internal static int Insert(BO.MasterAccount masterAccount)
		{
			var cmd =  @"
				insert into [MasterAccounts]
					([Name], [ProfileID], [DateCreated], [StatusEnum])
				values
					(@Name, @ProfileID, @DateCreated, @StatusEnum);
				
				select scope_identity();
				";
				
			var parameters = new SqlParameter[4]; 
			
			if (masterAccount.Name == null)
				parameters[0] = new SqlParameter("Name",DBNull.Value);
			else
				parameters[0] = new SqlParameter("Name",masterAccount.Name);
			parameters[1] = new SqlParameter("ProfileID",masterAccount.ProfileID );
			parameters[2] = new SqlParameter("DateCreated",masterAccount.DateCreated );
			parameters[3] = new SqlParameter("StatusEnum",masterAccount.StatusEnum );
			
			return Convert.ToInt32(DBHelper.GetScalar(cmd, CommandType.Text, parameters));
		}
		
		protected internal static void Update(BO.MasterAccount masterAccount)
		{
			var cmd = @"
				update [MasterAccounts] set
					Name = @Name, ProfileID = @ProfileID, DateCreated = @DateCreated, StatusEnum = @StatusEnum
				where ID = @ID
				";
				
			var parameters = new SqlParameter[5];
			parameters[0] = new SqlParameter("ID",masterAccount.ID );
			if (masterAccount.Name == null)
				parameters[1] = new SqlParameter("Name",DBNull.Value);
			else
				parameters[1] = new SqlParameter("Name",masterAccount.Name);
			parameters[2] = new SqlParameter("ProfileID",masterAccount.ProfileID );
			parameters[3] = new SqlParameter("DateCreated",masterAccount.DateCreated );
			parameters[4] = new SqlParameter("StatusEnum",masterAccount.StatusEnum );
			DBHelper.ExecuteNonQuery(cmd, CommandType.Text, parameters);
		}
		
		protected internal static void Delete(object ID)
		{
			var cmd = "delete [MasterAccounts] where ID = @ID";
			
			DBHelper.ExecuteNonQuery(cmd, CommandType.Text, new SqlParameter("ID", ID));
		}

		#endregion 
			
		internal static DataTable Select(string cmd, SqlParameter[] parameters)
		{
			return DBHelper.GetTable(cmd, parameters);
		}
		
		internal static int Count(string cmd, SqlParameter[] parameters)
		{
			return Convert.ToInt32(DBHelper.GetScalar(cmd, parameters));
		}
		
		internal static void SetValue(object id, string columnName, object value)
		{
			var cmd = string.Format("update [MasterAccounts] set [{0}] = @Value where ID = @ID", columnName);
			
			DBHelper.ExecuteNonQuery(cmd, new SqlParameter("Value", value ?? DBNull.Value), new SqlParameter("ID", id));
		}
		
		internal static void SafeDetach(object fkID, string columnName)
		{
			var cmd = string.Format("update [MasterAccounts] set [{0}] = null where [{0}] = @FkID", columnName);

			DBHelper.ExecuteNonQuery(cmd, new SqlParameter("FkID", fkID));
		}
	
	}
			
	
	public partial class Posts
	{

		#region Basic CRUD
		// Create / Update / Delete
		protected internal static long Insert(BO.Post post)
		{
			var cmd =  @"
				insert into [Posts]
					([EnumPostType], [AccountID], [PostingUserID], [TimePosted], [Title], [Text], [TwStatusId], [TwReplyNum], [TwRetweetNum], [FbItemID], [FbCommentNum], [FbShareNum], [FbLikeNum], [YtVideoId], [YtViewsNum], [YtCommentnum], [YtRating], [WpPostID], [WpCommentNum], [FlickrID], [FlickrUrl], [FlickrCommentNum], [StatusEnum])
				values
					(@EnumPostType, @AccountID, @PostingUserID, @TimePosted, @Title, @Text, @TwStatusId, @TwReplyNum, @TwRetweetNum, @FbItemID, @FbCommentNum, @FbShareNum, @FbLikeNum, @YtVideoId, @YtViewsNum, @YtCommentnum, @YtRating, @WpPostID, @WpCommentNum, @FlickrID, @FlickrUrl, @FlickrCommentNum, @StatusEnum);
				
				select scope_identity();
				";
				
			var parameters = new SqlParameter[23]; 
			
			parameters[0] = new SqlParameter("EnumPostType",post.EnumPostType );
			parameters[1] = new SqlParameter("AccountID",post.AccountID );
			if (post.PostingUserID == null)
				parameters[2] = new SqlParameter("PostingUserID",DBNull.Value);
			else
				parameters[2] = new SqlParameter("PostingUserID",post.PostingUserID);
			parameters[3] = new SqlParameter("TimePosted",post.TimePosted );
			if (post.Title == null)
				parameters[4] = new SqlParameter("Title",DBNull.Value);
			else
				parameters[4] = new SqlParameter("Title",post.Title);
			if (post.Text == null)
				parameters[5] = new SqlParameter("Text",DBNull.Value);
			else
				parameters[5] = new SqlParameter("Text",post.Text);
			if (post.TwStatusId == null)
				parameters[6] = new SqlParameter("TwStatusId",DBNull.Value);
			else
				parameters[6] = new SqlParameter("TwStatusId",post.TwStatusId);
			parameters[7] = new SqlParameter("TwReplyNum",post.TwReplyNum );
			parameters[8] = new SqlParameter("TwRetweetNum",post.TwRetweetNum );
			if (post.FbItemID == null)
				parameters[9] = new SqlParameter("FbItemID",DBNull.Value);
			else
				parameters[9] = new SqlParameter("FbItemID",post.FbItemID);
			parameters[10] = new SqlParameter("FbCommentNum",post.FbCommentNum );
			parameters[11] = new SqlParameter("FbShareNum",post.FbShareNum );
			parameters[12] = new SqlParameter("FbLikeNum",post.FbLikeNum );
			if (post.YtVideoId == null)
				parameters[13] = new SqlParameter("YtVideoId",DBNull.Value);
			else
				parameters[13] = new SqlParameter("YtVideoId",post.YtVideoId);
			parameters[14] = new SqlParameter("YtViewsNum",post.YtViewsNum );
			parameters[15] = new SqlParameter("YtCommentnum",post.YtCommentnum );
			parameters[16] = new SqlParameter("YtRating",post.YtRating );
			if (post.WpPostID == null)
				parameters[17] = new SqlParameter("WpPostID",DBNull.Value);
			else
				parameters[17] = new SqlParameter("WpPostID",post.WpPostID);
			parameters[18] = new SqlParameter("WpCommentNum",post.WpCommentNum );
			if (post.FlickrID == null)
				parameters[19] = new SqlParameter("FlickrID",DBNull.Value);
			else
				parameters[19] = new SqlParameter("FlickrID",post.FlickrID);
			if (post.FlickrUrl == null)
				parameters[20] = new SqlParameter("FlickrUrl",DBNull.Value);
			else
				parameters[20] = new SqlParameter("FlickrUrl",post.FlickrUrl);
			parameters[21] = new SqlParameter("FlickrCommentNum",post.FlickrCommentNum );
			parameters[22] = new SqlParameter("StatusEnum",post.StatusEnum );
			
			return Convert.ToInt64(DBHelper.GetScalar(cmd, CommandType.Text, parameters));
		}
		
		protected internal static void Update(BO.Post post)
		{
			var cmd = @"
				update [Posts] set
					EnumPostType = @EnumPostType, AccountID = @AccountID, PostingUserID = @PostingUserID, TimePosted = @TimePosted, Title = @Title, Text = @Text, TwStatusId = @TwStatusId, TwReplyNum = @TwReplyNum, TwRetweetNum = @TwRetweetNum, FbItemID = @FbItemID, FbCommentNum = @FbCommentNum, FbShareNum = @FbShareNum, FbLikeNum = @FbLikeNum, YtVideoId = @YtVideoId, YtViewsNum = @YtViewsNum, YtCommentnum = @YtCommentnum, YtRating = @YtRating, WpPostID = @WpPostID, WpCommentNum = @WpCommentNum, FlickrID = @FlickrID, FlickrUrl = @FlickrUrl, FlickrCommentNum = @FlickrCommentNum, StatusEnum = @StatusEnum
				where ID = @ID
				";
				
			var parameters = new SqlParameter[24];
			parameters[0] = new SqlParameter("ID",post.ID );
			parameters[1] = new SqlParameter("EnumPostType",post.EnumPostType );
			parameters[2] = new SqlParameter("AccountID",post.AccountID );
			if (post.PostingUserID == null)
				parameters[3] = new SqlParameter("PostingUserID",DBNull.Value);
			else
				parameters[3] = new SqlParameter("PostingUserID",post.PostingUserID);
			parameters[4] = new SqlParameter("TimePosted",post.TimePosted );
			if (post.Title == null)
				parameters[5] = new SqlParameter("Title",DBNull.Value);
			else
				parameters[5] = new SqlParameter("Title",post.Title);
			if (post.Text == null)
				parameters[6] = new SqlParameter("Text",DBNull.Value);
			else
				parameters[6] = new SqlParameter("Text",post.Text);
			if (post.TwStatusId == null)
				parameters[7] = new SqlParameter("TwStatusId",DBNull.Value);
			else
				parameters[7] = new SqlParameter("TwStatusId",post.TwStatusId);
			parameters[8] = new SqlParameter("TwReplyNum",post.TwReplyNum );
			parameters[9] = new SqlParameter("TwRetweetNum",post.TwRetweetNum );
			if (post.FbItemID == null)
				parameters[10] = new SqlParameter("FbItemID",DBNull.Value);
			else
				parameters[10] = new SqlParameter("FbItemID",post.FbItemID);
			parameters[11] = new SqlParameter("FbCommentNum",post.FbCommentNum );
			parameters[12] = new SqlParameter("FbShareNum",post.FbShareNum );
			parameters[13] = new SqlParameter("FbLikeNum",post.FbLikeNum );
			if (post.YtVideoId == null)
				parameters[14] = new SqlParameter("YtVideoId",DBNull.Value);
			else
				parameters[14] = new SqlParameter("YtVideoId",post.YtVideoId);
			parameters[15] = new SqlParameter("YtViewsNum",post.YtViewsNum );
			parameters[16] = new SqlParameter("YtCommentnum",post.YtCommentnum );
			parameters[17] = new SqlParameter("YtRating",post.YtRating );
			if (post.WpPostID == null)
				parameters[18] = new SqlParameter("WpPostID",DBNull.Value);
			else
				parameters[18] = new SqlParameter("WpPostID",post.WpPostID);
			parameters[19] = new SqlParameter("WpCommentNum",post.WpCommentNum );
			if (post.FlickrID == null)
				parameters[20] = new SqlParameter("FlickrID",DBNull.Value);
			else
				parameters[20] = new SqlParameter("FlickrID",post.FlickrID);
			if (post.FlickrUrl == null)
				parameters[21] = new SqlParameter("FlickrUrl",DBNull.Value);
			else
				parameters[21] = new SqlParameter("FlickrUrl",post.FlickrUrl);
			parameters[22] = new SqlParameter("FlickrCommentNum",post.FlickrCommentNum );
			parameters[23] = new SqlParameter("StatusEnum",post.StatusEnum );
			DBHelper.ExecuteNonQuery(cmd, CommandType.Text, parameters);
		}
		
		protected internal static void Delete(object ID)
		{
			var cmd = "delete [Posts] where ID = @ID";
			
			DBHelper.ExecuteNonQuery(cmd, CommandType.Text, new SqlParameter("ID", ID));
		}

		#endregion 
			
		internal static DataTable Select(string cmd, SqlParameter[] parameters)
		{
			return DBHelper.GetTable(cmd, parameters);
		}
		
		internal static int Count(string cmd, SqlParameter[] parameters)
		{
			return Convert.ToInt32(DBHelper.GetScalar(cmd, parameters));
		}
		
		internal static void SetValue(object id, string columnName, object value)
		{
			var cmd = string.Format("update [Posts] set [{0}] = @Value where ID = @ID", columnName);
			
			DBHelper.ExecuteNonQuery(cmd, new SqlParameter("Value", value ?? DBNull.Value), new SqlParameter("ID", id));
		}
		
		internal static void SafeDetach(object fkID, string columnName)
		{
			var cmd = string.Format("update [Posts] set [{0}] = null where [{0}] = @FkID", columnName);

			DBHelper.ExecuteNonQuery(cmd, new SqlParameter("FkID", fkID));
		}
	
	}
			
	
	public partial class PostStats
	{

		#region Basic CRUD
		// Create / Update / Delete
		protected internal static long Insert(BO.PostStat postStat)
		{
			var cmd =  @"
				insert into [PostStats]
					([PostID], [Date], [TwReplyNum], [TwRetweetNum], [FbCommentNum], [FbShareNum], [FbLikeNum], [YtViewsNum], [YtCommentnum], [YtRating], [WpCommentNum], [FlickrCommentNum], [StatusEnum])
				values
					(@PostID, @Date, @TwReplyNum, @TwRetweetNum, @FbCommentNum, @FbShareNum, @FbLikeNum, @YtViewsNum, @YtCommentnum, @YtRating, @WpCommentNum, @FlickrCommentNum, @StatusEnum);
				
				select scope_identity();
				";
				
			var parameters = new SqlParameter[13]; 
			
			parameters[0] = new SqlParameter("PostID",postStat.PostID );
			parameters[1] = new SqlParameter("Date",postStat.Date );
			parameters[2] = new SqlParameter("TwReplyNum",postStat.TwReplyNum );
			parameters[3] = new SqlParameter("TwRetweetNum",postStat.TwRetweetNum );
			parameters[4] = new SqlParameter("FbCommentNum",postStat.FbCommentNum );
			parameters[5] = new SqlParameter("FbShareNum",postStat.FbShareNum );
			parameters[6] = new SqlParameter("FbLikeNum",postStat.FbLikeNum );
			parameters[7] = new SqlParameter("YtViewsNum",postStat.YtViewsNum );
			parameters[8] = new SqlParameter("YtCommentnum",postStat.YtCommentnum );
			parameters[9] = new SqlParameter("YtRating",postStat.YtRating );
			parameters[10] = new SqlParameter("WpCommentNum",postStat.WpCommentNum );
			parameters[11] = new SqlParameter("FlickrCommentNum",postStat.FlickrCommentNum );
			parameters[12] = new SqlParameter("StatusEnum",postStat.StatusEnum );
			
			return Convert.ToInt64(DBHelper.GetScalar(cmd, CommandType.Text, parameters));
		}
		
		protected internal static void Update(BO.PostStat postStat)
		{
			var cmd = @"
				update [PostStats] set
					PostID = @PostID, Date = @Date, TwReplyNum = @TwReplyNum, TwRetweetNum = @TwRetweetNum, FbCommentNum = @FbCommentNum, FbShareNum = @FbShareNum, FbLikeNum = @FbLikeNum, YtViewsNum = @YtViewsNum, YtCommentnum = @YtCommentnum, YtRating = @YtRating, WpCommentNum = @WpCommentNum, FlickrCommentNum = @FlickrCommentNum, StatusEnum = @StatusEnum
				where ID = @ID
				";
				
			var parameters = new SqlParameter[14];
			parameters[0] = new SqlParameter("ID",postStat.ID );
			parameters[1] = new SqlParameter("PostID",postStat.PostID );
			parameters[2] = new SqlParameter("Date",postStat.Date );
			parameters[3] = new SqlParameter("TwReplyNum",postStat.TwReplyNum );
			parameters[4] = new SqlParameter("TwRetweetNum",postStat.TwRetweetNum );
			parameters[5] = new SqlParameter("FbCommentNum",postStat.FbCommentNum );
			parameters[6] = new SqlParameter("FbShareNum",postStat.FbShareNum );
			parameters[7] = new SqlParameter("FbLikeNum",postStat.FbLikeNum );
			parameters[8] = new SqlParameter("YtViewsNum",postStat.YtViewsNum );
			parameters[9] = new SqlParameter("YtCommentnum",postStat.YtCommentnum );
			parameters[10] = new SqlParameter("YtRating",postStat.YtRating );
			parameters[11] = new SqlParameter("WpCommentNum",postStat.WpCommentNum );
			parameters[12] = new SqlParameter("FlickrCommentNum",postStat.FlickrCommentNum );
			parameters[13] = new SqlParameter("StatusEnum",postStat.StatusEnum );
			DBHelper.ExecuteNonQuery(cmd, CommandType.Text, parameters);
		}
		
		protected internal static void Delete(object ID)
		{
			var cmd = "delete [PostStats] where ID = @ID";
			
			DBHelper.ExecuteNonQuery(cmd, CommandType.Text, new SqlParameter("ID", ID));
		}

		#endregion 
			
		internal static DataTable Select(string cmd, SqlParameter[] parameters)
		{
			return DBHelper.GetTable(cmd, parameters);
		}
		
		internal static int Count(string cmd, SqlParameter[] parameters)
		{
			return Convert.ToInt32(DBHelper.GetScalar(cmd, parameters));
		}
		
		internal static void SetValue(object id, string columnName, object value)
		{
			var cmd = string.Format("update [PostStats] set [{0}] = @Value where ID = @ID", columnName);
			
			DBHelper.ExecuteNonQuery(cmd, new SqlParameter("Value", value ?? DBNull.Value), new SqlParameter("ID", id));
		}
		
		internal static void SafeDetach(object fkID, string columnName)
		{
			var cmd = string.Format("update [PostStats] set [{0}] = null where [{0}] = @FkID", columnName);

			DBHelper.ExecuteNonQuery(cmd, new SqlParameter("FkID", fkID));
		}
	
	}
			
	
	public partial class Profiles
	{

		#region Basic CRUD
		// Create / Update / Delete
		protected internal static int Insert(BO.Profile profile)
		{
			var cmd =  @"
				insert into [Profiles]
					([Name], [DateCreated], [StatusEnum])
				values
					(@Name, @DateCreated, @StatusEnum);
				
				select scope_identity();
				";
				
			var parameters = new SqlParameter[3]; 
			
			if (profile.Name == null)
				parameters[0] = new SqlParameter("Name",DBNull.Value);
			else
				parameters[0] = new SqlParameter("Name",profile.Name);
			if (profile.DateCreated == null)
				parameters[1] = new SqlParameter("DateCreated",DBNull.Value);
			else
				parameters[1] = new SqlParameter("DateCreated",profile.DateCreated);
			parameters[2] = new SqlParameter("StatusEnum",profile.StatusEnum );
			
			return Convert.ToInt32(DBHelper.GetScalar(cmd, CommandType.Text, parameters));
		}
		
		protected internal static void Update(BO.Profile profile)
		{
			var cmd = @"
				update [Profiles] set
					Name = @Name, DateCreated = @DateCreated, StatusEnum = @StatusEnum
				where ID = @ID
				";
				
			var parameters = new SqlParameter[4];
			parameters[0] = new SqlParameter("ID",profile.ID );
			if (profile.Name == null)
				parameters[1] = new SqlParameter("Name",DBNull.Value);
			else
				parameters[1] = new SqlParameter("Name",profile.Name);
			if (profile.DateCreated == null)
				parameters[2] = new SqlParameter("DateCreated",DBNull.Value);
			else
				parameters[2] = new SqlParameter("DateCreated",profile.DateCreated);
			parameters[3] = new SqlParameter("StatusEnum",profile.StatusEnum );
			DBHelper.ExecuteNonQuery(cmd, CommandType.Text, parameters);
		}
		
		protected internal static void Delete(object ID)
		{
			var cmd = "delete [Profiles] where ID = @ID";
			
			DBHelper.ExecuteNonQuery(cmd, CommandType.Text, new SqlParameter("ID", ID));
		}

		#endregion 
			
		internal static DataTable Select(string cmd, SqlParameter[] parameters)
		{
			return DBHelper.GetTable(cmd, parameters);
		}
		
		internal static int Count(string cmd, SqlParameter[] parameters)
		{
			return Convert.ToInt32(DBHelper.GetScalar(cmd, parameters));
		}
		
		internal static void SetValue(object id, string columnName, object value)
		{
			var cmd = string.Format("update [Profiles] set [{0}] = @Value where ID = @ID", columnName);
			
			DBHelper.ExecuteNonQuery(cmd, new SqlParameter("Value", value ?? DBNull.Value), new SqlParameter("ID", id));
		}
		
		internal static void SafeDetach(object fkID, string columnName)
		{
			var cmd = string.Format("update [Profiles] set [{0}] = null where [{0}] = @FkID", columnName);

			DBHelper.ExecuteNonQuery(cmd, new SqlParameter("FkID", fkID));
		}
	
	}
			
	
	public partial class TracedAccounts
	{

		#region Basic CRUD
		// Create / Update / Delete
		protected internal static int Insert(BO.TracedAccount tracedAccount)
		{
			var cmd =  @"
				insert into [TracedAccounts]
					([AccountID], [EnumType], [Name], [ImageUrl], [FbPageID], [FbPageUrl], [FanNum], [StartDate], [StatusEnum])
				values
					(@AccountID, @EnumType, @Name, @ImageUrl, @FbPageID, @FbPageUrl, @FanNum, @StartDate, @StatusEnum);
				
				select scope_identity();
				";
				
			var parameters = new SqlParameter[9]; 
			
			parameters[0] = new SqlParameter("AccountID",tracedAccount.AccountID );
			parameters[1] = new SqlParameter("EnumType",tracedAccount.EnumType );
			if (tracedAccount.Name == null)
				parameters[2] = new SqlParameter("Name",DBNull.Value);
			else
				parameters[2] = new SqlParameter("Name",tracedAccount.Name);
			if (tracedAccount.ImageUrl == null)
				parameters[3] = new SqlParameter("ImageUrl",DBNull.Value);
			else
				parameters[3] = new SqlParameter("ImageUrl",tracedAccount.ImageUrl);
			if (tracedAccount.FbPageID == null)
				parameters[4] = new SqlParameter("FbPageID",DBNull.Value);
			else
				parameters[4] = new SqlParameter("FbPageID",tracedAccount.FbPageID);
			if (tracedAccount.FbPageUrl == null)
				parameters[5] = new SqlParameter("FbPageUrl",DBNull.Value);
			else
				parameters[5] = new SqlParameter("FbPageUrl",tracedAccount.FbPageUrl);
			if (tracedAccount.FanNum == null)
				parameters[6] = new SqlParameter("FanNum",DBNull.Value);
			else
				parameters[6] = new SqlParameter("FanNum",tracedAccount.FanNum);
			parameters[7] = new SqlParameter("StartDate",tracedAccount.StartDate );
			parameters[8] = new SqlParameter("StatusEnum",tracedAccount.StatusEnum );
			
			return Convert.ToInt32(DBHelper.GetScalar(cmd, CommandType.Text, parameters));
		}
		
		protected internal static void Update(BO.TracedAccount tracedAccount)
		{
			var cmd = @"
				update [TracedAccounts] set
					AccountID = @AccountID, EnumType = @EnumType, Name = @Name, ImageUrl = @ImageUrl, FbPageID = @FbPageID, FbPageUrl = @FbPageUrl, FanNum = @FanNum, StartDate = @StartDate, StatusEnum = @StatusEnum
				where ID = @ID
				";
				
			var parameters = new SqlParameter[10];
			parameters[0] = new SqlParameter("ID",tracedAccount.ID );
			parameters[1] = new SqlParameter("AccountID",tracedAccount.AccountID );
			parameters[2] = new SqlParameter("EnumType",tracedAccount.EnumType );
			if (tracedAccount.Name == null)
				parameters[3] = new SqlParameter("Name",DBNull.Value);
			else
				parameters[3] = new SqlParameter("Name",tracedAccount.Name);
			if (tracedAccount.ImageUrl == null)
				parameters[4] = new SqlParameter("ImageUrl",DBNull.Value);
			else
				parameters[4] = new SqlParameter("ImageUrl",tracedAccount.ImageUrl);
			if (tracedAccount.FbPageID == null)
				parameters[5] = new SqlParameter("FbPageID",DBNull.Value);
			else
				parameters[5] = new SqlParameter("FbPageID",tracedAccount.FbPageID);
			if (tracedAccount.FbPageUrl == null)
				parameters[6] = new SqlParameter("FbPageUrl",DBNull.Value);
			else
				parameters[6] = new SqlParameter("FbPageUrl",tracedAccount.FbPageUrl);
			if (tracedAccount.FanNum == null)
				parameters[7] = new SqlParameter("FanNum",DBNull.Value);
			else
				parameters[7] = new SqlParameter("FanNum",tracedAccount.FanNum);
			parameters[8] = new SqlParameter("StartDate",tracedAccount.StartDate );
			parameters[9] = new SqlParameter("StatusEnum",tracedAccount.StatusEnum );
			DBHelper.ExecuteNonQuery(cmd, CommandType.Text, parameters);
		}
		
		protected internal static void Delete(object ID)
		{
			var cmd = "delete [TracedAccounts] where ID = @ID";
			
			DBHelper.ExecuteNonQuery(cmd, CommandType.Text, new SqlParameter("ID", ID));
		}

		#endregion 
			
		internal static DataTable Select(string cmd, SqlParameter[] parameters)
		{
			return DBHelper.GetTable(cmd, parameters);
		}
		
		internal static int Count(string cmd, SqlParameter[] parameters)
		{
			return Convert.ToInt32(DBHelper.GetScalar(cmd, parameters));
		}
		
		internal static void SetValue(object id, string columnName, object value)
		{
			var cmd = string.Format("update [TracedAccounts] set [{0}] = @Value where ID = @ID", columnName);
			
			DBHelper.ExecuteNonQuery(cmd, new SqlParameter("Value", value ?? DBNull.Value), new SqlParameter("ID", id));
		}
		
		internal static void SafeDetach(object fkID, string columnName)
		{
			var cmd = string.Format("update [TracedAccounts] set [{0}] = null where [{0}] = @FkID", columnName);

			DBHelper.ExecuteNonQuery(cmd, new SqlParameter("FkID", fkID));
		}
	
	}
			
	
	public partial class TraceStats
	{

		#region Basic CRUD
		// Create / Update / Delete
		protected internal static long Insert(BO.TraceStat traceStat)
		{
			var cmd =  @"
				insert into [TraceStats]
					([TraceAccountID], [Date], [FanNum])
				values
					(@TraceAccountID, @Date, @FanNum);
				
				select scope_identity();
				";
				
			var parameters = new SqlParameter[3]; 
			
			if (traceStat.TraceAccountID == null)
				parameters[0] = new SqlParameter("TraceAccountID",DBNull.Value);
			else
				parameters[0] = new SqlParameter("TraceAccountID",traceStat.TraceAccountID);
			parameters[1] = new SqlParameter("Date",traceStat.Date );
			parameters[2] = new SqlParameter("FanNum",traceStat.FanNum );
			
			return Convert.ToInt64(DBHelper.GetScalar(cmd, CommandType.Text, parameters));
		}
		
		protected internal static void Update(BO.TraceStat traceStat)
		{
			var cmd = @"
				update [TraceStats] set
					TraceAccountID = @TraceAccountID, Date = @Date, FanNum = @FanNum
				where ID = @ID
				";
				
			var parameters = new SqlParameter[4];
			parameters[0] = new SqlParameter("ID",traceStat.ID );
			if (traceStat.TraceAccountID == null)
				parameters[1] = new SqlParameter("TraceAccountID",DBNull.Value);
			else
				parameters[1] = new SqlParameter("TraceAccountID",traceStat.TraceAccountID);
			parameters[2] = new SqlParameter("Date",traceStat.Date );
			parameters[3] = new SqlParameter("FanNum",traceStat.FanNum );
			DBHelper.ExecuteNonQuery(cmd, CommandType.Text, parameters);
		}
		
		protected internal static void Delete(object ID)
		{
			var cmd = "delete [TraceStats] where ID = @ID";
			
			DBHelper.ExecuteNonQuery(cmd, CommandType.Text, new SqlParameter("ID", ID));
		}

		#endregion 
			
		internal static DataTable Select(string cmd, SqlParameter[] parameters)
		{
			return DBHelper.GetTable(cmd, parameters);
		}
		
		internal static int Count(string cmd, SqlParameter[] parameters)
		{
			return Convert.ToInt32(DBHelper.GetScalar(cmd, parameters));
		}
		
		internal static void SetValue(object id, string columnName, object value)
		{
			var cmd = string.Format("update [TraceStats] set [{0}] = @Value where ID = @ID", columnName);
			
			DBHelper.ExecuteNonQuery(cmd, new SqlParameter("Value", value ?? DBNull.Value), new SqlParameter("ID", id));
		}
		
		internal static void SafeDetach(object fkID, string columnName)
		{
			var cmd = string.Format("update [TraceStats] set [{0}] = null where [{0}] = @FkID", columnName);

			DBHelper.ExecuteNonQuery(cmd, new SqlParameter("FkID", fkID));
		}
	
	}
			
	
	public partial class UnProcessedPosts
	{

		#region Basic CRUD
		// Create / Update / Delete
		protected internal static long Insert(BO.UnProcessedPost unProcessedPost)
		{
			var cmd =  @"
				insert into [UnProcessedPosts]
					([PostID], [EnumStage], [PostOnFb], [PostOnTw], [PostOnWp], [Comment], [StatusEnum])
				values
					(@PostID, @EnumStage, @PostOnFb, @PostOnTw, @PostOnWp, @Comment, @StatusEnum);
				
				select scope_identity();
				";
				
			var parameters = new SqlParameter[7]; 
			
			parameters[0] = new SqlParameter("PostID",unProcessedPost.PostID );
			parameters[1] = new SqlParameter("EnumStage",unProcessedPost.EnumStage );
			if (unProcessedPost.PostOnFb == null)
				parameters[2] = new SqlParameter("PostOnFb",DBNull.Value);
			else
				parameters[2] = new SqlParameter("PostOnFb",unProcessedPost.PostOnFb);
			if (unProcessedPost.PostOnTw == null)
				parameters[3] = new SqlParameter("PostOnTw",DBNull.Value);
			else
				parameters[3] = new SqlParameter("PostOnTw",unProcessedPost.PostOnTw);
			if (unProcessedPost.PostOnWp == null)
				parameters[4] = new SqlParameter("PostOnWp",DBNull.Value);
			else
				parameters[4] = new SqlParameter("PostOnWp",unProcessedPost.PostOnWp);
			if (unProcessedPost.Comment == null)
				parameters[5] = new SqlParameter("Comment",DBNull.Value);
			else
				parameters[5] = new SqlParameter("Comment",unProcessedPost.Comment);
			parameters[6] = new SqlParameter("StatusEnum",unProcessedPost.StatusEnum );
			
			return Convert.ToInt64(DBHelper.GetScalar(cmd, CommandType.Text, parameters));
		}
		
		protected internal static void Update(BO.UnProcessedPost unProcessedPost)
		{
			var cmd = @"
				update [UnProcessedPosts] set
					PostID = @PostID, EnumStage = @EnumStage, PostOnFb = @PostOnFb, PostOnTw = @PostOnTw, PostOnWp = @PostOnWp, Comment = @Comment, StatusEnum = @StatusEnum
				where ID = @ID
				";
				
			var parameters = new SqlParameter[8];
			parameters[0] = new SqlParameter("ID",unProcessedPost.ID );
			parameters[1] = new SqlParameter("PostID",unProcessedPost.PostID );
			parameters[2] = new SqlParameter("EnumStage",unProcessedPost.EnumStage );
			if (unProcessedPost.PostOnFb == null)
				parameters[3] = new SqlParameter("PostOnFb",DBNull.Value);
			else
				parameters[3] = new SqlParameter("PostOnFb",unProcessedPost.PostOnFb);
			if (unProcessedPost.PostOnTw == null)
				parameters[4] = new SqlParameter("PostOnTw",DBNull.Value);
			else
				parameters[4] = new SqlParameter("PostOnTw",unProcessedPost.PostOnTw);
			if (unProcessedPost.PostOnWp == null)
				parameters[5] = new SqlParameter("PostOnWp",DBNull.Value);
			else
				parameters[5] = new SqlParameter("PostOnWp",unProcessedPost.PostOnWp);
			if (unProcessedPost.Comment == null)
				parameters[6] = new SqlParameter("Comment",DBNull.Value);
			else
				parameters[6] = new SqlParameter("Comment",unProcessedPost.Comment);
			parameters[7] = new SqlParameter("StatusEnum",unProcessedPost.StatusEnum );
			DBHelper.ExecuteNonQuery(cmd, CommandType.Text, parameters);
		}
		
		protected internal static void Delete(object ID)
		{
			var cmd = "delete [UnProcessedPosts] where ID = @ID";
			
			DBHelper.ExecuteNonQuery(cmd, CommandType.Text, new SqlParameter("ID", ID));
		}

		#endregion 
			
		internal static DataTable Select(string cmd, SqlParameter[] parameters)
		{
			return DBHelper.GetTable(cmd, parameters);
		}
		
		internal static int Count(string cmd, SqlParameter[] parameters)
		{
			return Convert.ToInt32(DBHelper.GetScalar(cmd, parameters));
		}
		
		internal static void SetValue(object id, string columnName, object value)
		{
			var cmd = string.Format("update [UnProcessedPosts] set [{0}] = @Value where ID = @ID", columnName);
			
			DBHelper.ExecuteNonQuery(cmd, new SqlParameter("Value", value ?? DBNull.Value), new SqlParameter("ID", id));
		}
		
		internal static void SafeDetach(object fkID, string columnName)
		{
			var cmd = string.Format("update [UnProcessedPosts] set [{0}] = null where [{0}] = @FkID", columnName);

			DBHelper.ExecuteNonQuery(cmd, new SqlParameter("FkID", fkID));
		}
	
	}
			
	
	public partial class Users
	{

		#region Basic CRUD
		// Create / Update / Delete
		protected internal static int Insert(BO.User user)
		{
			var cmd =  @"
				insert into [Users]
					([Email], [Username], [Password], [FirstName], [LastName], [ProfileID], [LastModified], [StatusEnum])
				values
					(@Email, @Username, @Password, @FirstName, @LastName, @ProfileID, @LastModified, @StatusEnum);
				
				select scope_identity();
				";
				
			var parameters = new SqlParameter[8]; 
			
			if (user.Email == null)
				parameters[0] = new SqlParameter("Email",DBNull.Value);
			else
				parameters[0] = new SqlParameter("Email",user.Email);
			if (user.Username == null)
				parameters[1] = new SqlParameter("Username",DBNull.Value);
			else
				parameters[1] = new SqlParameter("Username",user.Username);
			if (user.Password == null)
				parameters[2] = new SqlParameter("Password",DBNull.Value);
			else
				parameters[2] = new SqlParameter("Password",user.Password);
			if (user.FirstName == null)
				parameters[3] = new SqlParameter("FirstName",DBNull.Value);
			else
				parameters[3] = new SqlParameter("FirstName",user.FirstName);
			if (user.LastName == null)
				parameters[4] = new SqlParameter("LastName",DBNull.Value);
			else
				parameters[4] = new SqlParameter("LastName",user.LastName);
			if (user.ProfileID == null)
				parameters[5] = new SqlParameter("ProfileID",DBNull.Value);
			else
				parameters[5] = new SqlParameter("ProfileID",user.ProfileID);
			parameters[6] = new SqlParameter("LastModified",user.LastModified );
			parameters[7] = new SqlParameter("StatusEnum",user.StatusEnum );
			
			return Convert.ToInt32(DBHelper.GetScalar(cmd, CommandType.Text, parameters));
		}
		
		protected internal static void Update(BO.User user)
		{
			var cmd = @"
				update [Users] set
					Email = @Email, Username = @Username, Password = @Password, FirstName = @FirstName, LastName = @LastName, ProfileID = @ProfileID, LastModified = @LastModified, StatusEnum = @StatusEnum
				where ID = @ID
				";
				
			var parameters = new SqlParameter[9];
			parameters[0] = new SqlParameter("ID",user.ID );
			if (user.Email == null)
				parameters[1] = new SqlParameter("Email",DBNull.Value);
			else
				parameters[1] = new SqlParameter("Email",user.Email);
			if (user.Username == null)
				parameters[2] = new SqlParameter("Username",DBNull.Value);
			else
				parameters[2] = new SqlParameter("Username",user.Username);
			if (user.Password == null)
				parameters[3] = new SqlParameter("Password",DBNull.Value);
			else
				parameters[3] = new SqlParameter("Password",user.Password);
			if (user.FirstName == null)
				parameters[4] = new SqlParameter("FirstName",DBNull.Value);
			else
				parameters[4] = new SqlParameter("FirstName",user.FirstName);
			if (user.LastName == null)
				parameters[5] = new SqlParameter("LastName",DBNull.Value);
			else
				parameters[5] = new SqlParameter("LastName",user.LastName);
			if (user.ProfileID == null)
				parameters[6] = new SqlParameter("ProfileID",DBNull.Value);
			else
				parameters[6] = new SqlParameter("ProfileID",user.ProfileID);
			parameters[7] = new SqlParameter("LastModified",user.LastModified );
			parameters[8] = new SqlParameter("StatusEnum",user.StatusEnum );
			DBHelper.ExecuteNonQuery(cmd, CommandType.Text, parameters);
		}
		
		protected internal static void Delete(object ID)
		{
			var cmd = "delete [Users] where ID = @ID";
			
			DBHelper.ExecuteNonQuery(cmd, CommandType.Text, new SqlParameter("ID", ID));
		}

		#endregion 
			
		internal static DataTable Select(string cmd, SqlParameter[] parameters)
		{
			return DBHelper.GetTable(cmd, parameters);
		}
		
		internal static int Count(string cmd, SqlParameter[] parameters)
		{
			return Convert.ToInt32(DBHelper.GetScalar(cmd, parameters));
		}
		
		internal static void SetValue(object id, string columnName, object value)
		{
			var cmd = string.Format("update [Users] set [{0}] = @Value where ID = @ID", columnName);
			
			DBHelper.ExecuteNonQuery(cmd, new SqlParameter("Value", value ?? DBNull.Value), new SqlParameter("ID", id));
		}
		
		internal static void SafeDetach(object fkID, string columnName)
		{
			var cmd = string.Format("update [Users] set [{0}] = null where [{0}] = @FkID", columnName);

			DBHelper.ExecuteNonQuery(cmd, new SqlParameter("FkID", fkID));
		}
	
	}
			
}



